const logger = require("../logger")
const { verify } = require("../utils/auth")
const response = require('../utils/response')
function authMiddleware(ctx,next){
    const token = ctx.headers['authorization']
    if(token !== undefined && token !== ''){
        const {error,admin} = verify(token)
        if(error !== null){
            logger.error("登录错误" + error.message)
            response.error(ctx,'登录过期请重新登录' + error.message,4000)
            return
        }else{
			console.log('获取登录信息如下')
			console.log(admin)
            ctx.userInfo = admin._doc //直接将用户信息挂上去
            return next()
        }
    }else{
        response.error(ctx,'token不能为空',4000)
        return
    }
}
module.exports = authMiddleware